package edu.jd.block_chain.security.login;

import edu.jd.block_chain.common.Result;
import edu.jd.block_chain.common.TokenUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @descriptions:
 * @data: 2021/11/23 10:29
 */
@RestController
@RequestMapping("/security/login")
public class LoginAPI {
    @Resource
    private LoginService loginService;

    @PostMapping("")//表示直接用父地址,只接收post类型
    public Result login(@RequestBody LoginDto dto){

        boolean ok = loginService.checkLogin(dto);

        if (ok){
            //生成一个登录令牌
            String token = TokenUtils.loginSign(dto.getU_id(),dto.getU_pwd());
            return Result.success((Object)token);//向客户端发送成功型号，并发送令牌
        }else{
            return Result.fail(Result.ERR_CODE_PWD,"账号或密码错误");
        }
    }
}
